﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySpaceID.SDK.Context;
using MySpaceID.SDK.Config;
using Jayrock.Json.Conversion;
using MySpaceID.SDK.Models.V2;

namespace MySpaceID.SDK.Api
{
    /// <summary>
    /// <para>PortableContacts is the class that encapsulates all the work you will need to make server-to-server calls to MySpace Portable Contacts REST Resources.</para>
    /// <para>Please refer to http://wiki.developer.myspace.com/index.php?title=Portable_Contacts_REST_Resources for full details on Portable Contacts</para>
    /// </summary>
    public class PortableContacts : BaseApi
    {
        public PortableContacts(SecurityContext context) : base(context) { }

        #region API Methods

        /// <summary>
        /// <para>Returns the Person Resource per the Portable Contacts specification.</para>
        /// <para>See more details here: http://wiki.developer.myspace.com/index.php?title=Portable_Contacts_REST_Resources#Person_Resource</para>
        /// <para>Resource: /v2/people/@me/@self</para>
        /// </summary>
        /// <returns></returns>
        public Self GetSelf()
        {
            return GetSelf(null);
        }

        /// <summary>
        /// <para>Returns the Person Resource per the Portable Contacts specification.</para>
        /// <para>See more details here: http://wiki.developer.myspace.com/index.php?title=Portable_Contacts_REST_Resources#Person_Resource</para>
        /// <para>Resource: /v2/people/@me/@self?fields={fields}</para>
        /// </summary>
        /// <param name="fields">List of fields that are desired from the Person Resource. See details for fields here: http://wiki.developer.myspace.com/index.php?title=Portable_Contacts_REST_Resources#The_fields_Parameter</param>
        /// <returns></returns>
        public Self GetSelf(List<string> fields)
        {
            var uri = string.Empty;
            if (fields == null)
                uri = V2Endpoints.SELF_URL;
            else
            {
                var listOfFiles = string.Empty;
                foreach (var field in fields)
                {
                    listOfFiles += string.Format("{0},", field);
                }
                listOfFiles = listOfFiles.Remove(listOfFiles.Length - 1);
                uri = string.Format(V2Endpoints.SELF_WITH_FIELDS_URL, listOfFiles);
            }
            return (Self)JsonConvert.Import(typeof(Self), this.Context.MakeRequest(uri, MySpaceID.SDK.OAuth.Common.Enums.ResponseFormatType.JSON,
                MySpaceID.SDK.OAuth.Common.Enums.HttpMethodType.GET, null, false));
        }

        /// <summary>
        /// <para>Returns the Friend Resource per the Portable Contacts specification.</para>
        /// <para>See more details here: http://wiki.developer.myspace.com/index.php?title=Portable_Contacts_REST_Resources#The_Friends_Resource</para>
        /// <para>Resource: /v2/people/@me/@friends</para>
        /// </summary>
        /// <returns></returns>
        public Friends GetFriends()
        {
            return GetFriends(null, null);
        }

        /// <summary>
        /// <para>Returns the Friend Resource per the Portable Contacts specification.</para>
        /// <para>See more details here: http://wiki.developer.myspace.com/index.php?title=Portable_Contacts_REST_Resources#The_Friends_Resource</para>
        /// <para>Resource: /v2/people/@me/@friends</para>
        /// </summary>
        /// <param name="startIndex">The index into the paged friends collection</param>
        /// <param name="count">The number of records in the friends collection</param>
        /// <returns></returns>
        public Friends GetFriends(int? startIndex, int? count)
        {
            string uri = V2Endpoints.FRIENDS_URL;
            if (startIndex.HasValue && count.HasValue)
            {
                ValidatePageParameters(startIndex.Value, count.Value);
                uri += string.Format("?{0}={1}&{2}={3}", Constants.START_INDEX, startIndex, Constants.COUNT, count);
            }
            return (Friends)JsonConvert.Import(typeof(Friends), this.Context.MakeRequest(uri, MySpaceID.SDK.OAuth.Common.Enums.ResponseFormatType.JSON,
                MySpaceID.SDK.OAuth.Common.Enums.HttpMethodType.GET, null, false));

        }

        #endregion
    }
}
